import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.regex.Pattern;


public class ExOnFile {
	
	//HashMap<String,String> globalProperties;
	
	public static void main(String[] args) throws IOException {
		FileInputStream fileInputStream = null;
		File file = new File("C:\\RK\\Localdebugging\\rama.txt");
		fileInputStream = new FileInputStream(file);
		fileInputStream.close();
		if(fileInputStream != null){
			System.out.println("true");
		}
		
		File newLocation = new File("C:\\RK\\rama.txt");
		Date date = new Date(newLocation.lastModified()); 
		System.out.println("filecreatedtime"+date.toString());
		System.out.println(newLocation.getParentFile()+"parentlocation");
		System.out.println(newLocation.getPath()+"parentlocation");
		File sourceFile = new File("afasdfnasdf"+newLocation.getPath()+"baccc");
		System.out.println(sourceFile.getPath());
	//	boolean flag = file.renameTo(newLocation);
		//System.out.println(flag);
		if(file.exists()){
			System.out.println("file is there"+file.getPath());
		}else{
			//System.out.println(file.getParent());
			File file2 = new File(file.getParent());
		File fileArr[] = file2.listFiles();
		for(int i=0; i<fileArr.length; i++){
			File file3 =fileArr[i];
			if(file3.getName().trim().equals("ram.txt")){
				File newLocationFile = new File(newLocation.getParentFile().getPath()+file3.separator+file3.getName());
				System.out.println(newLocation.getAbsolutePath());
				file3.renameTo(newLocationFile);
				System.out.println("successfully moved"+file3.separator);
			}
		}
		}
		
		
		
		//start
		
		
		
		
		
		
		
		
		
		
		/*if(file.length() !=0){
			//System.out.println(file.getUsableSpace());
			System.out.println("hiiiiiii");
		}
		*/
		/*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
		File file = new File("C:\\RK\\Localdebugging");
		File global = new File("C:\\RK\\Localdebugging\\global.xml");
		String path ="/app/cbf/GLRD/OPERATIONS/archive/RES/fkof{date(yyyymmddhhmm)}.res";
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("1", path);
		String hmPath = hm.get("1");
		
		String modifiedPath = hmPath.replace("{date(yyyymmddhhmm)}", dateFormat.format(new Date().getTime()));
		System.out.println(modifiedPath);
		loadGlobalProperties(global);*/
		
		/*File file = new File("C:\\Temp\\FOFDB.kz");
		File file2 = new File("C:\\RK\\FOFDB.kz");
		String path ="/app/cbf/GLRD/OPERATIONS/archive/RES/fkof{date(yyyymmddhhmm)}.res";
		//replaceString(path);
		//System.out.println(modifiedpath);
		file.renameTo(file2);//for moving file from one location to another location
		int i =12345;
		Random r = new Random();
		int j=r.nextInt(i);
		System.out.println("Random Values"+j);
		System.out.println(file.getName());*/
	}
	
	public static void loadGlobalProperties(File file) throws IOException
	{
    	
    	
		HashMap<String,String> globalProperties = new HashMap<String, String>();
		try {
			FileInputStream fileInput = new FileInputStream(file);
			Properties properties = new Properties();
			properties.loadFromXML(fileInput);
			fileInput.close();

			/*Set<Object> set = properties.keySet();
			Iterator<Object>  i = set.iterator();*/
			Enumeration enuKeys =properties.keys();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
			String timestamp =dateFormat.format(new Date().getTime());
			/*while(i.hasNext()){
				String key = (String) i.next();
				String value = (String)properties.getProperty(key);
				String modifiedString = value.replaceAll(Pattern.quote("{date(yyyymmddhhmm)}"), timestamp);//('{', 'R');//("{date(yyyyMMddhhmm)}", "1");
				System.out.println(modifiedString);
				globalProperties.put(key, modifiedString);
			}*/
			while (enuKeys.hasMoreElements()) {
				String key = (String) enuKeys.nextElement();
				String value = properties.getProperty(key);
				String modifiedString = value.replaceAll(Pattern.quote("{date(yyyymmddhhmm)}"), timestamp);//('{', 'R');//("{date(yyyyMMddhhmm)}", "1");
				System.out.println(modifiedString);
				globalProperties.put(key, modifiedString);
			}
			
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		}
	}

   
    public static String resolveDate(String input)
    {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
		String timestamp =dateFormat.format(new Date().getTime());
		System.out.println(input.replace("{date(yyyyMMddhhmm)}", timestamp));
		return input.replace("{date(yyyyMMddhhmm)}", timestamp);
    }
}
